package com.sanjiahantang.youjiarole.module.me.order.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.evaluate.views.impl.EvaluateGoodsActivity;
import com.sanjiahantang.youjiarole.module.invoice.views.InvoiceView;
import com.sanjiahantang.youjiarole.module.invoice.views.impl.InvoiceActivity;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiarole.module.me.address.views.AddressView;
import com.sanjiahantang.youjiarole.module.me.order.beans.CreateOrderPost;
import com.sanjiahantang.youjiarole.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiarole.module.me.order.presenters.OrderPresenter;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;


/**
 * Created by zy on 2017/9/27.
 */
public class ServiceOrderActivity extends BaseMvpActivity<OrderView,OrderPresenter> implements View.OnClickListener,
        OrderView {

    private View panelCreate,panelPay,panelEvaluate,rlInvoiceInfo,panelInvoice;
    private TextView txtState;//状态文字
    private ImageView ivState;//状态图标
    private TextView txtOrderNum;//订单号
    private TextView txtCreateTime;//创建时间
    private ListView goodListView;//列表
    private TextView txtGoodsCount;//数量
    private TextView txtAmount;//价格
    private EditText etConsumerMessage;//留言
    private TextView txtTotal;//合计
    private TextView btnPayForStateUnPay;//应付金额
    private ServiceListBean.Data serviceData;
    private TextView title;
    private TextView txtSubmit;

    private boolean isRefreshing = false;
    private int type;

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_order_info);
        title = (TextView) findViewById(R.id.title);
        title.setText("订单详情");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        panelCreate = findViewById(R.id.panelCreate);
        panelPay = findViewById(R.id.panelPay);
        panelEvaluate = findViewById(R.id.panelEvaluate);

        txtState = (TextView) findViewById(R.id.txtState);
        ivState = (ImageView) findViewById(R.id.ivState);
        txtOrderNum = (TextView) findViewById(R.id.txtOrderNum);
        txtCreateTime = (TextView) findViewById(R.id.txtCreateTime);
        goodListView = (ListView) findViewById(R.id.goodListView);
        txtGoodsCount = (TextView) findViewById(R.id.txtGoodsCount);
        txtAmount = (TextView) findViewById(R.id.txtAmount);
        etConsumerMessage = (EditText) findViewById(R.id.etConsumerMessage);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        btnPayForStateUnPay = (TextView) findViewById(R.id.btnPayForStateUnPay);
        panelInvoice = findViewById(R.id.panelInvoice);



        txtSubmit = (TextView) findViewById(R.id.txtSubmit);
        txtSubmit.setOnClickListener(this);
        findViewById(R.id.btnEvaluate).setOnClickListener(this);

        type = getIntent().getIntExtra(KEY_TYPE,0);
        switch (type){
            case ORDER_TYPE_CANCEL:
                panelPay.setVisibility(View.VISIBLE);
                break;
            case ORDER_TYPE_PAY:
                panelPay.setVisibility(View.VISIBLE);
                break;
            case ORDER_TYPE_CLOSE:
                break;
            case ORDER_TYPE_PAYED:
                break;
            case ORDER_TYPE_COMPLETE:
                panelEvaluate.setVisibility(View.VISIBLE);
                break;
            case ORDER_TYPE_PART_PAY:
                panelCreate.setVisibility(View.VISIBLE);
                break;
        }


    }



    //我的订单
    private void initGoodsOrder() {
        Bundle b = getIntent().getBundleExtra(KEY_PARAM);
        ServiceListBean.Data goods = (ServiceListBean.Data) b.getSerializable("goods");
        presenter.searchServiceOrder($this, goods.getOrderCode(), new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                title.setText("订单详情");
                serviceData = (ServiceListBean.Data) content;
                String statusName = serviceData.getStatusName();

                if (serviceData.getServiceType() == 3){
                    panelInvoice.setVisibility(View.GONE);
                }else if(serviceData.getStatusId() == OrderView.TYPE_PAYED || serviceData.getStatusId() == OrderView.TYPE_EVALUATED){
                    panelInvoice.setVisibility(View.VISIBLE);
                }else{
                    panelInvoice.setVisibility(View.GONE);
                }
                int resId = R.drawable.i_unpay_head;
                switch (serviceData.getStatusId()){
                    case OrderView.TYPE_UN_PAY:
                        statusName = "待付款";
                        resId = R.drawable.i_unpay_head;
                        panelPay.setVisibility(View.VISIBLE);
                        panelCreate.setVisibility(View.GONE);
                        panelEvaluate.setVisibility(View.GONE);
                        break;
                    case OrderView.TYPE_PART_PAY:
                        resId = R.drawable.i_unpay_head;
                        panelPay.setVisibility(View.GONE);
                        panelCreate.setVisibility(View.VISIBLE);
                        panelEvaluate.setVisibility(View.GONE);

                        txtTotal.setText(String.valueOf(serviceData.getPayResult()));
                        txtSubmit.setText("去付款");
                        break;
                    case OrderView.TYPE_PAYED:
                        resId = R.drawable.i_payed_head;
                        panelPay.setVisibility(View.GONE);
                        panelCreate.setVisibility(View.GONE);

                        if (serviceData.getServiceType() == 3 && serviceData != null && serviceData.getServiceRecordList().get(0).getPlanOrderState() == 1){
                            panelEvaluate.setVisibility(View.VISIBLE);
                            findViewById(R.id.llBottom).setVisibility(View.VISIBLE);
                        }else if(serviceData.getServiceType() != 3){
                            panelEvaluate.setVisibility(View.VISIBLE);
                            findViewById(R.id.llBottom).setVisibility(View.VISIBLE);
                        }else{
                            findViewById(R.id.llBottom).setVisibility(View.GONE);
                        }

                        break;
                    case OrderView.TYPE_COMPLETE:
                        statusName = "已完成";
                        resId = R.drawable.i_payed_head;
                        findViewById(R.id.llBottom).setVisibility(View.GONE);
                        break;
                    case OrderView.TYPE_CLOSE:
                        resId = R.drawable.i_unpay_head;
                        findViewById(R.id.llBottom).setVisibility(View.GONE);
                        break;
                    case OrderView.TYPE_EVALUATED:
                        statusName = "已完成";
                        resId = R.drawable.i_unpay_head;
                        findViewById(R.id.llBottom).setVisibility(View.GONE);
                        break;
                    default:
                        resId = R.drawable.i_unpay_head;
                        findViewById(R.id.llBottom).setVisibility(View.GONE);
                        break;
                }
                txtState.setText(statusName);
                ivState.setImageResource(resId);
                txtOrderNum.setText(serviceData.getOrderCode());
                if (serviceData.getCreateTime() != null){
                    txtCreateTime.setText(serviceData.getCreateTime().substring(0,"yyyy-MM-dd".length()));
                }

                goodListView.setAdapter(new QuickAdapter<ServiceListBean.Data.ServiceRecordListBean>($this,R.layout.item_serviceorderitem,serviceData.getServiceRecordList()) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, ServiceListBean.Data.ServiceRecordListBean item) {
                        helper.setText(R.id.sg_name,item.getServiceName());
                        helper.setText(R.id.sg_price,String.valueOf(item.getPrice() * item.getCount()));
//                        helper.setText(R.id.sg_count,String.valueOf(item.getCount()));
                    }
                });

                int count = 0;//数量
                float price = 0f;
                for(ServiceListBean.Data.ServiceRecordListBean item : serviceData.getServiceRecordList()){
                    count += item.getCount();
                    price += Float.parseFloat(String.valueOf(item.getResult()));
                }
                txtAmount.setText(String.format("%.2f",price));
                txtTotal.setText(String.format("%.2f",serviceData.getPayResult()));
                txtGoodsCount.setText(String.valueOf(serviceData.getServiceRecordList().size()));
                btnPayForStateUnPay.setText(String.format("本次应付%.2f元",(float)serviceData.getPayResult()));


                //处理发票
                CreateOrderPost.OrderInvoiceInfoDtoBean invoice = serviceData.getOrderInvoiceInfoDto();
                if(invoice == null || invoice.getOiiId() == 0){
                    panelInvoice.findViewById(R.id.rlNoInvoice).setVisibility(View.VISIBLE);
                    panelInvoice.findViewById(R.id.rlInvoiceInfo).setVisibility(View.GONE);
                }else {
                    panelInvoice.findViewById(R.id.rlNoInvoice).setVisibility(View.GONE);
                    panelInvoice.findViewById(R.id.rlInvoiceInfo).setVisibility(View.VISIBLE);

                    TextView txtInvoiceTitle = (TextView) panelInvoice.findViewById(R.id.txtInvoiceTitle);
                    TextView txtInvoiceTaxNo = (TextView) panelInvoice.findViewById(R.id.txtInvoiceTaxNo);
                    if (!TextUtils.isEmpty(invoice.getTaxNum())){
                        panelInvoice.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.VISIBLE);
                        txtInvoiceTaxNo.setText(invoice.getTaxNum());
                    }else {
                        panelInvoice.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.GONE);
                    }
                    txtInvoiceTitle.setText(invoice.getInvoiceTitle());
                }

                if (serviceData != null && (serviceData.getOrderInvoiceInfoDto() == null || serviceData.getOrderInvoiceInfoDto().getOiiId() == 0)){
                    panelInvoice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent mIntent = new Intent($this, InvoiceActivity.class);
                            mIntent.putExtra("orderCode",serviceData.getOrderCode());
                            mIntent.putExtra("orderType",1);
                            float money = 0;
                            for (ServiceListBean.Data.ServiceRecordListBean item : serviceData.getServiceRecordList()){
                                money += item.getResult();
                            }
                            mIntent.putExtra("money",money);
                            startActivityForResult(mIntent, OrderView.INTENT_INVOICE);
                        }
                    });
                }else{
                    panelInvoice.setOnClickListener(null);
                }

            }
        });

        btnPayForStateUnPay.setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        initGoodsOrder();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == INTENT_INVOICE && resultCode == RESULT_OK){
            if(data != null){
                presenter.setInvoice(data.getStringExtra(KEY_TITLE),data.getStringExtra(InvoiceView.KEY_TAX_NO));
            }
        }
        if (requestCode == INTENT_ADDRESS && resultCode == RESULT_OK){
            if(data != null){
                AddressBean.Data address = new AddressBean.Data();
                address.setCustomerName(data.getStringExtra(AddressView.KEY_NAME));
                address.setFullAddress(data.getStringExtra(AddressView.KEY_ADDRESS));
                address.setCellPhone(data.getStringExtra(AddressView.KEY_PHONE));
                presenter.setAddress(address);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;

        }
    }


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
    }


}
